package com.rc.saas.tenant.service.ugs;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.elec.ElecStationMapper;
import com.rc.saas.tenant.mapper.ugs.TenantUgsLevelDiscountRecordMapper;
import com.rc.saas.tenant.mapper.ugs.TenantUgsLevelDiscountStationMapper;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelDiscountRecord;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelDiscountStation;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelDiscountStationExample;
import com.rc.saas.tenant.search.ugs.LevelDiscountStationSearch;
import com.rc.saas.tenant.vo.ugs.LevelDiscountStationResultVo;
import com.rc.saas.tenant.vo.ugs.UgsLevelDiscountStationResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author frog
 * @date 2021/4/7
 */
@Service
public class TenantUgsLevelDiscountStationServiceImpl implements TenantUgsLevelDiscountStationService {

    @Autowired
    private TenantUgsLevelDiscountStationMapper tenantUgsLevelDiscountStationMapper;

    @Autowired
    private TenantUgsLevelDiscountRecordMapper tenantUgsLevelDiscountRecordMapper;

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Override
    public TenantUgsLevelDiscountStationMapper getMapper() {
        return tenantUgsLevelDiscountStationMapper;
    }

    @Override
    public PageInfo<LevelDiscountStationResultVo> listPageLevelDiscountStation(LevelDiscountStationSearch levelDiscountStationSearch) {
        PageHelper.startPage(levelDiscountStationSearch.getOffset(), levelDiscountStationSearch.getLimit());
        List<LevelDiscountStationResultVo> levelDiscountStationResultVOS = tenantUgsLevelDiscountStationMapper.listPageLevelDiscountStation(levelDiscountStationSearch);
        return new PageInfo<LevelDiscountStationResultVo>(levelDiscountStationResultVOS);
    }

    @Override
    public UgsLevelDiscountStationResultVo selectDiscountByType(String discountType, Long elecStationId) {
        TenantUgsLevelDiscountStationExample example = new TenantUgsLevelDiscountStationExample();
        example.createCriteria().andElecStationIdEqualTo(elecStationId);
        List<TenantUgsLevelDiscountStation> tenantUgsLevelDiscountStations = tenantUgsLevelDiscountStationMapper.selectByExample(example);
        ElecStation elecStation = elecStationMapper.selectByPrimaryKey(elecStationId);
        UgsLevelDiscountStationResultVo ugsLevelDiscountStationResultVo = new UgsLevelDiscountStationResultVo();
        ugsLevelDiscountStationResultVo.setStationName(elecStation.getStationName());
        ugsLevelDiscountStationResultVo.setElecStationId(elecStationId);
        if (!CollectionUtils.isEmpty(tenantUgsLevelDiscountStations)) {
            if ("0".equals(discountType)) {
                for (TenantUgsLevelDiscountStation tenantUgsLevelDiscountStation : tenantUgsLevelDiscountStations) {
                    if ("L1".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        ugsLevelDiscountStationResultVo.setStatus(tenantUgsLevelDiscountStation.getStatus());
                        ugsLevelDiscountStationResultVo.setBeginTime(tenantUgsLevelDiscountStation.getBeginTime());
                        ugsLevelDiscountStationResultVo.setEndTime(tenantUgsLevelDiscountStation.getEndTime());
                        ugsLevelDiscountStationResultVo.setL1(tenantUgsLevelDiscountStation.getDiscountRatio());
                    } else if ("L2".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        ugsLevelDiscountStationResultVo.setL2(tenantUgsLevelDiscountStation.getDiscountRatio());
                    } else if ("L3".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        ugsLevelDiscountStationResultVo.setL3(tenantUgsLevelDiscountStation.getDiscountRatio());
                    } else if ("L4".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        ugsLevelDiscountStationResultVo.setL4(tenantUgsLevelDiscountStation.getDiscountRatio());
                    } else if ("L5".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        ugsLevelDiscountStationResultVo.setL5(tenantUgsLevelDiscountStation.getDiscountRatio());
                    }
                }
                return ugsLevelDiscountStationResultVo;
            } else if ("1".equals(discountType)) {
                for (TenantUgsLevelDiscountStation tenantUgsLevelDiscountStation : tenantUgsLevelDiscountStations) {
                    if ("V1".equals(tenantUgsLevelDiscountStation.getSuperLevelType())) {
                        ugsLevelDiscountStationResultVo.setStatus(tenantUgsLevelDiscountStation.getStatus());
                        ugsLevelDiscountStationResultVo.setBeginTime(tenantUgsLevelDiscountStation.getBeginTime());
                        ugsLevelDiscountStationResultVo.setEndTime(tenantUgsLevelDiscountStation.getEndTime());
                        ugsLevelDiscountStationResultVo.setV1(tenantUgsLevelDiscountStation.getDiscountRatio());
                    } else if ("V2".equals(tenantUgsLevelDiscountStation.getSuperLevelType())) {
                        ugsLevelDiscountStationResultVo.setV2(tenantUgsLevelDiscountStation.getDiscountRatio());
                    } else if ("V3".equals(tenantUgsLevelDiscountStation.getSuperLevelType())) {
                        ugsLevelDiscountStationResultVo.setV3(tenantUgsLevelDiscountStation.getDiscountRatio());
                    }
                }
                return ugsLevelDiscountStationResultVo;
            }
        }
        return null;
    }

    @Override
    public void saveOrUpdateStationDiscount(UgsLevelDiscountStationResultVo ugsLevelDiscountStationResultVo) {
        TenantUgsLevelDiscountRecord tenantUgsLevelDiscountRecord = new TenantUgsLevelDiscountRecord();

        //增加记录
        tenantUgsLevelDiscountRecord.setTenantCode(ugsLevelDiscountStationResultVo.getTenantCode());
        tenantUgsLevelDiscountRecord.setElecStationId(ugsLevelDiscountStationResultVo.getElecStationId());
        tenantUgsLevelDiscountRecord.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
        tenantUgsLevelDiscountRecord.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
        tenantUgsLevelDiscountRecord.setRecordType(ugsLevelDiscountStationResultVo.getStatus());
        //默认为0
        tenantUgsLevelDiscountRecord.setDiscountType("0");

        TenantUgsLevelDiscountStationExample example = new TenantUgsLevelDiscountStationExample();
        example.createCriteria().andElecStationIdEqualTo(ugsLevelDiscountStationResultVo.getElecStationId());
        List<TenantUgsLevelDiscountStation> tenantUgsLevelDiscountStations = tenantUgsLevelDiscountStationMapper.selectByExample(example);

        // todo 根据类型长度判断是做会员超级会员的保存编辑操作
        if ("0".equals(ugsLevelDiscountStationResultVo.getDiscountType())) {
            if (tenantUgsLevelDiscountStations.size() != 5 && tenantUgsLevelDiscountStations.size() != 8) {
                tenantUgsLevelDiscountStations.clear();
            }
        }else if ("1".equals(ugsLevelDiscountStationResultVo.getDiscountType())){
            if (tenantUgsLevelDiscountStations.size() != 3 && tenantUgsLevelDiscountStations.size() != 8) {
                tenantUgsLevelDiscountStations.clear();
            }
        }

        if (!CollectionUtils.isEmpty(tenantUgsLevelDiscountStations)) {
            for (TenantUgsLevelDiscountStation tenantUgsLevelDiscountStation : tenantUgsLevelDiscountStations) {
                if ("0".equals(ugsLevelDiscountStationResultVo.getDiscountType())) {
                    if ("L1".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL1());


                        tenantUgsLevelDiscountRecord.setLevelType("L1");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL1());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                    } else if ("L2".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL2());

                        tenantUgsLevelDiscountRecord.setLevelType("L2");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL2());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    } else if ("L3".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL3());

                        tenantUgsLevelDiscountRecord.setLevelType("L3");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL3());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    } else if ("L4".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL4());

                        tenantUgsLevelDiscountRecord.setLevelType("L4");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL4());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    } else if ("L5".equals(tenantUgsLevelDiscountStation.getLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL5());

                        tenantUgsLevelDiscountRecord.setLevelType("L5");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL5());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    }
                } else if ("1".equals(ugsLevelDiscountStationResultVo.getDiscountType())) {
                    if ("V1".equals(tenantUgsLevelDiscountStation.getSuperLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getV1());

                        //增加记录
                        tenantUgsLevelDiscountRecord.setTenantCode(tenantUgsLevelDiscountStation.getTenantCode());
                        tenantUgsLevelDiscountRecord.setElecStationId(ugsLevelDiscountStationResultVo.getElecStationId());
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getV1());
                        tenantUgsLevelDiscountRecord.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountRecord.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountRecord.setRecordType(ugsLevelDiscountStationResultVo.getStatus());
                        //默认为0
                        tenantUgsLevelDiscountRecord.setDiscountType("0");
                        //等级会员卡类型 0月卡(30天)  1季卡(90天) 2年卡(365天) ,跟ugs_super_level_price表一样
                        tenantUgsLevelDiscountRecord.setSuperCardType("0");
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    } else if ("V2".equals(tenantUgsLevelDiscountStation.getSuperLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getV2());

                        tenantUgsLevelDiscountRecord.setSuperCardType("1");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getV2());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    } else if ("V3".equals(tenantUgsLevelDiscountStation.getSuperLevelType())) {
                        tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());
                        tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
                        tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
                        tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getV3());

                        tenantUgsLevelDiscountRecord.setSuperCardType("2");
                        tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getV3());
                        tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
                    }
                }
                tenantUgsLevelDiscountStationMapper.updateByPrimaryKeySelective(tenantUgsLevelDiscountStation);
            }
        } else {

            TenantUgsLevelDiscountStation tenantUgsLevelDiscountStation = new TenantUgsLevelDiscountStation();
            tenantUgsLevelDiscountStation.setTenantCode(ugsLevelDiscountStationResultVo.getTenantCode());
            tenantUgsLevelDiscountStation.setElecStationId(ugsLevelDiscountStationResultVo.getElecStationId());
            tenantUgsLevelDiscountStation.setDiscountType("0");
            tenantUgsLevelDiscountStation.setBeginTime(ugsLevelDiscountStationResultVo.getBeginTime());
            tenantUgsLevelDiscountStation.setEndTime(ugsLevelDiscountStationResultVo.getEndTime());
            tenantUgsLevelDiscountStation.setStatus(ugsLevelDiscountStationResultVo.getStatus());

            if ("0".equals(ugsLevelDiscountStationResultVo.getDiscountType())) {
                tenantUgsLevelDiscountStation.setLevelType("L1");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL1());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setLevelType("L1");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL1());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                tenantUgsLevelDiscountStation.setLevelType("L2");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL2());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setLevelType("L2");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL2());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                tenantUgsLevelDiscountStation.setLevelType("L3");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL3());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setLevelType("L3");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL3());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                tenantUgsLevelDiscountStation.setLevelType("L4");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL4());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setLevelType("L4");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL4());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                tenantUgsLevelDiscountStation.setLevelType("L5");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getL5());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setLevelType("L5");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getL5());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

            } else if ("1".equals(ugsLevelDiscountStationResultVo.getDiscountType())) {
                tenantUgsLevelDiscountStation.setSuperLevelType("V1");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getV1());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setSuperCardType("0");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getV1());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                tenantUgsLevelDiscountStation.setSuperLevelType("V2");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getV2());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setSuperCardType("1");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getV2());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);

                tenantUgsLevelDiscountStation.setSuperLevelType("V3");
                tenantUgsLevelDiscountStation.setDiscountRatio(ugsLevelDiscountStationResultVo.getV3());
                tenantUgsLevelDiscountStationMapper.insertSelective(tenantUgsLevelDiscountStation);

                tenantUgsLevelDiscountRecord.setSuperCardType("2");
                tenantUgsLevelDiscountRecord.setDiscountRatio(ugsLevelDiscountStationResultVo.getV3());
                tenantUgsLevelDiscountRecordMapper.insertSelective(tenantUgsLevelDiscountRecord);
            }

        }
    }
}
